// 导入express依赖

const express = require('express')
const db = require('./config/db.js')
const bodyParser = require('body-parser');

// 引入jwt token工具
const JwtUtil = require('./utils/jwt');
// 解决跨域模块
const cors = require('cors');

// 创建App应用

const app = express()
// 测试
app.get('/', function (req, res) {
    res.send(`<h1>测试成功</h1>`)
})

//解析json编码数据
app.use(bodyParser.json());
//解析url编码的数据
app.use(bodyParser.urlencoded({ extended: false }));

// 跨域访问
// 跨域资源共享 (Cross-origin resource sharing)
app.use(cors());

app.all("*", function (req, res, next) {
    //设置允许跨域的域名，*代表允许任意域名跨域
    res.header("Access-Control-Allow-Origin", "*");
    //允许的header类型
    res.header("Access-Contro1-Allow-Headers", "content-type");
    //跨域允许的请求方式
    res.header("Access-Control-Allow-Methods", "DELETE,PUT,POST,GET,OPTIONS");
    if (req.method.toLowerCase() == 'options ')
        res.send(200); //让options尝试请求快速结束else
    next();
})





// 导入用户路由
const userRouter = require('./router/user/index.js');
app.use('/user', userRouter);

// 导入任务路由
const taskRouter = require('./router/task/index.js');
app.use('/task', taskRouter);

const myTaskRouter = require('./router/myTask/index.js');
app.use('/myTask', myTaskRouter);


app.use('/static', express.static('public'));
app.use('/static',express.static('public'));


// 监听服务的端口
app.listen(3007, () => {
    console.log(`服务启动了，访问http:localhost:3007`);
})